<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('科目名称')" prop="keyName">
            <el-input v-model="form.keyName" :placeholder="$t('科目名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <span style="display: none;">key_code: {{ form.keyCode }}</span>
          <el-form-item :label="$t('财报类型')" prop="belongsTable">
            <el-select v-model="form.belongsTable" :placeholder="$t('财报类型')">
              <el-option
                v-for="dict in tableTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('重点关注')" prop="focusOn">
            <el-radio-group v-model="form.focusOn" :placeholder="$t('重点关注')">
              <el-radio label="Y">{{ $t('是') }}</el-radio>
              <el-radio label="N">{{ $t('否') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('数据类型')" prop="keyType">
            <el-radio-group v-model="form.keyType" :placeholder="$t('数据类型')">
              <el-radio label="money">{{ $t('金额') }}</el-radio>
              <el-radio label="ratio">{{ $t('百分比') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="keySort">
            <el-input v-model="form.sort" :placeholder="$t('排序')" show-word-limit />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <el-button @click="cancel">{{ $t("取消") }}</el-button>
      <el-button v-hasPermi="['company:pcw:edit']" type="primary" @click="submitForm">{{ $t("保存") }}</el-button>
    </div>
  </div>
</template>

<script>
import { savePcwTempSysSubject, getPcwTempSysSubject, verifyByKeyName } from '@/api/company/pcw/pcwTempSysSubject'
export default {
  props: {
    typeId: {
      type: String,
      required: true,
      default: ''
    }
  },
  data() {
    // 科目名称唯一校验
    var checkKeyName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('必填信息'))
      } else if (value && this.form.belongsTable) {
        const row = {
          id: this.form.id ? this.form.id : null,
          typeId: this.form.typeId,
          belongsTable: this.form.belongsTable,
          keyName: value
        }
        verifyByKeyName(row).then(res => {
          console.log(res, 'resresres')
          if (res.msg) {
            callback(new Error('已存在该数据'))
          } else {
            callback()
          }
        })
      }
    }
    return {
      // 表类型字典
      tableTypeOptions: [],
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        belongsTable: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        keyName: [{ required: true, validator: checkKeyName, trigger: ['blur', 'change'] }],
        sort: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }]
      }
    }
  },
  created() {
    this.getDicts('pcw_table_type').then(response => {
      this.tableTypeOptions = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      if (data !== null && data.id !== undefined) {
        getPcwTempSysSubject(data.id).then(response => {
          this.form = response.data
        })
      } else {
        this.reset()
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        typeId: this.typeId,
        keyName: undefined,
        keyCode: undefined,
        keySort: undefined,
        tableType: this.form.tableType,
        keyType: 'money',
        focusOn: 'N',
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      this.$parent.$parent.formOpen = false
      // 刷新list页面
      this.$parent.$parent.getList()
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          savePcwTempSysSubject(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    }

  }

}
</script>
